/*
 * Copyright (c) 2011-2024, Cpjit (cpjit@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cpjit.data.autoconfigure;

import com.cpjit.data.web.MyBatisPlusPageHandlerMethodArgumentResolver;
import com.cpjit.data.web.config.DataMyBatisWebConfiguration;
import com.cpjit.data.web.config.MyBatisPlusPageHandlerMethodArgumentResolverCustomizer;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.web.config.SortHandlerMethodArgumentResolverCustomizer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author yonghuan[cpjit@qq.com]
 */
@Import(DataMyBatisWebConfiguration.class)
@ConditionalOnClass({MyBatisPlusPageHandlerMethodArgumentResolver.class, WebMvcConfigurer.class})
@EnableConfigurationProperties(SpringDataWebProperties.class)
@AllArgsConstructor
public class DataMyBatisWebAutoConfiguration {

    private final SpringDataWebProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public MyBatisPlusPageHandlerMethodArgumentResolverCustomizer pageHandlerMethodArgumentResolverCustomizer() {
        return (pageableResolver, bodyArgumentResolver) -> {
            SpringDataWebProperties.Pageable pageable = this.properties.getPageable();
            if (pageableResolver != null) {
                pageableResolver.setPageParameterName(pageable.getPageParameter());
                pageableResolver.setSizeParameterName(pageable.getSizeParameter());
                pageableResolver.setOneIndexedParameters(pageable.isOneIndexedParameters());
            }
            if (bodyArgumentResolver != null) {
                bodyArgumentResolver.setPageParameterName(pageable.getPageParameter());
                bodyArgumentResolver.setSizeParameterName(pageable.getSizeParameter());
                bodyArgumentResolver.setOneIndexedParameters(pageable.isOneIndexedParameters());
            }
        };
    }

    @Bean
    @ConditionalOnMissingBean
    public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
        return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
    }

}
